from django.core import serializers
from django.forms.models import model_to_dict
from django.shortcuts import render
from django.http import JsonResponse
from django.http import HttpResponse
from index import models
from index.service import userEncodeService

# Create your views here.
def index(request):
    data = {
        'foo': 'bar',
        'baz': 'qux',
    }
    return JsonResponse(data)



# 获取用户（读操作）
def getUser(request):
    users = models.UserInfo.objects.all()
    d = []
    for i in users:
        d.append(model_to_dict(i))
    userEncodeService.userEncode(d)
    return JsonResponse(d, safe=False)



def createUser(request):
    openId = request.POST.get('openId')
    models.UserInfo.objects.using("default").create(openId=openId)
    return HttpResponse('创建用户')